﻿package cn.antscript.airCMS.display.component
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author antscript
	 */
	public class ScrollPanel extends Sprite
	{
		
		private var _hander:Sprite;
		private var _container:Sprite;
		private var _mask:Sprite;
		
		private var _w:int;
		private var _h:int;
		
		private var _displayObj:DisplayObject;
		
		public function ScrollPanel(handerBitmapData:BitmapData=null,container:DisplayObjectContainer=null,xPos:int=0,yPos:int=0) 
		{
			_container = new Sprite();
			addChild(_container);
			
			_mask = new Sprite();
			addChild(_mask);
			_container.mask = _mask;
			
			
			
			_hander = new Sprite();
			addChild(_hander);
			_hander.mouseChildren = false;
			_hander.buttonMode = true;
			_hander.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			
			
			
			if (handerBitmapData == null) {
				_hander.graphics.beginFill(0xff0000, 1);
				_hander.graphics.drawRect(0, 0, 15, 40);
				_hander.graphics.endFill();
			}
			else {
				var bitmap:Bitmap = new Bitmap(handerBitmapData);
				_hander.addChild(bitmap);
			}
			
			
			if (container != null) {
				container.addChild(this);
			}
			this.x = xPos;
			this.y = yPos;
			
			
			
			
		}
		
		private function onDown(e:MouseEvent):void 
		{
			_hander.startDrag(false, new Rectangle(_w, 0, 0, _h - _hander.height));
			
			stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
			addEventListener(Event.ENTER_FRAME, onEnter);
		}
		
		private function onEnter(e:Event):void 
		{
			_displayObj.y = -(_displayObj.height - _h) * (_hander.y / (_h - _hander.height));
		}
		
		private function onUp(e:MouseEvent):void 
		{
			removeEventListener(Event.ENTER_FRAME, onEnter);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			_hander.stopDrag();
		}
		public function setSize(w:int = 500, h:int=100):void {
			
			_w = w;
			_h = h;
			
			_mask.graphics.clear();
			_mask.graphics.beginFill(0x0, 1);
			_mask.graphics.drawRect(0, 0, w , h);
			_mask.graphics.endFill();
			
			
			
			graphics.clear();
			graphics.lineStyle(2, 0xaaaaaa, 0.8);							
			graphics.beginFill(0x000000,0.4);
			graphics.drawRect( -6, -6, _w+_hander.width+11 , h+11 );
			graphics.endFill();
			
			
			
			_hander.x = w;
			_hander.y = 0;	
			
			_displayObj.x = 0;
			_displayObj.y = 0;		
		}
		
		public function setup(displayObj:DisplayObject, w:int = 500, h:int=100):void {
			
			
			if (_displayObj != null) {
				_container.removeChild(_displayObj);
				_displayObj = null;
			}
			
			_displayObj = displayObj;
			
			
			_container.addChildAt(displayObj, 0);
			
			setSize(w, h);
			
			_hander.visible = (displayObj.height > _h);
			
			if (_hander.visible) {
				addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
			}
		}
		
		private function onWheel(e:MouseEvent):void 
		{
			_hander.y -= e.delta*3;
			if (_hander.y >= _h - _hander.height) {
				_hander.y = _h - _hander.height
			}
			else if (_hander.y < 0) {
				_hander.y = 0;
			}
			_displayObj.y = -(_displayObj.height - _h) * (_hander.y / (_h - _hander.height));
		}
		
		public function update():void {
			
			if (_hander.visible) {
				removeEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
			}
			
			setSize(_w, _h);
			
			_hander.visible = (_displayObj.height > _h); 
			
			if (_hander.visible) {
				addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
			}
		}
		
	}

}